package com.my.main.controller;

import com.my.main.base.controller.BaseController;
import com.my.main.base.entity.Result;
import com.my.main.base.entity.ResultCode;
import com.my.main.service.userAndRight.SysUserService;
import com.my.main.shiro.token.UserToken;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RequestMapping(value="/sys")
@RestController
public class LoginController extends BaseController {


    @Autowired
    private SysUserService sysUserService;


    /**
     * 登录
     * @param loginMap
     * @return
     */
    @ApiOperation(value="系统 - 本地用户登录接口", tags = NOINTERCEPT)
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> loginMap) {

        String phone = loginMap.get("phone");
        String password = loginMap.get("password");

        try {
            //1.获取subject
            Subject subject = SecurityUtils.getSubject();
            //2.调用login方法，进入realm完成认证

            subject.login(new UserToken(phone, password ,"User"));

            //subject.login(upToken);
            //3.获取sessionId
            //subject.getSession().setTimeout(36000000);
            String sessionId = (String)subject.getSession().getId();
            //4.构造返回结果
            return new Result(ResultCode.SUCCESS,sessionId, "操作成功！");
        } catch (Exception e) {
            return new Result(ResultCode.MOBILEORPASSWORDERROR);
        }

    }

}
